
#pragma once
#include<iostream>
#include<assert.h>
#include<string>
#include<assert.h>
using namespace std;

namespace jy
{
	class string
	{
	public:

		typedef char* iterator;
		typedef const char* const_iterator;
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		
		/*string(const string& s)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}*/

		string(const string& s)
		{
			string tmp(s._str);
			swap(tmp);
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		/*string& operator=(const string& str)
		{
			if (this != &str)
			{
				delete[] _str;
				_str = new char[_capacity + 1];
				strcpy(_str, str._str);
				_size = str._size;
				_capacity = str._capacity;
			}

			return *this;
		}*/

		string& operator=(string tmp)
		{
			swap(tmp);
			return *this;
		}

		iterator begin()
		{
			return _str;
		}
		
		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin()const
		{
			return _str;
		}
		const_iterator end()const
		{
			return _str + _size;
		}

		const char* c_str()const
		{
			return _str;
		}
		
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		void swap(string& s1)
		{
			std::swap(_str, s1._str);
			std::swap(_size, s1._size);
			std::swap(_capacity, s1._capacity);
		}
	
		size_t size()const
		{
			return _size;
		}


		size_t capacity()const
		{
			return _capacity;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}


		void push_back(char c);
		void reserve(size_t n);
		string& operator+=(const char* ch);
		string& operator+=(char ch);
		void append(const char* str);
		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* str);
		void earse(size_t pos, size_t len = npos);
		size_t find(char ch, size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);
		string substr(size_t pos = 0, size_t len  = npos);
	
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;

		static const size_t npos;
	};

	bool operator<(const string& s1, const string& s2);
	bool operator<=(const string& s1, const string& s2);
	bool operator>(const string& s1, const string& s2);
	bool operator>=(const string& s1, const string& s2);
	bool operator==(const string& s1, const string& s2);
	bool operator!=(const string& s1, const string& s2);

	ostream& operator<<(ostream& out, const string& s1);
	istream& operator>>(istream& in, string& s1);

}